import fetch from "src/utils/fetch";
import API from "src/utils/api";
import notification from "src/utils/notification";


const Handler = {
  state: {

    handlerList:{
      list:[],
    },

  },
  mutations: {
    // handler列表数据
    SetHandlerListData(state,data){
      state.handlerList.list=data.handlerInfoList ? data.handlerInfoList : [];
    },
  },
  actions: {

    // handler 新增-编辑-删除
    doHandlerName: ({ state, commit }, params) => {

      let url='';

      if(params.cb){
        var cb=params.cb;
        delete params.cb;
      }

      if(params.type == 'add' || params.type == 'edit'){
        url=API.batch.handlerOperateInfo;
      }else if(params.type == 'del'){
        url=API.batch.handlerDeletedInfo;
        params.data={handlerName: params.data.handlerName};
      }else{
        return;
      }

      fetch({
        commit: commit,
        url: url,
        type: "post",
        data: params.data,
        showRegionLoading: true,
        success: (data)=>{
          notification.success('操作成功!');
          cb && cb();
          this.a.actions.getHandlerList({state, commit },{});
        },
      });
    },

    getHandlerList: ({ state, commit }, params) => {

      let cb;
      if(params.cb){
        cb=params.cb;
        delete params.cb;
      }

      fetch({
        commit: commit,
        url: API.batch.handlerList,
        type: "post",
        data: params,
        showRegionLoading: true,
        success: (data)=>{
          cb ? cb(data) : commit("SetHandlerListData", data);
        },
      });
    },
  },
  getters: {

    handlerList:(state, rootState)=>state.handlerList,

  }
};

export default Handler;
